package ru.mcfr.oxygen.framework.listeners;

import ro.sync.ecss.extensions.api.AuthorAccess;
import ro.sync.ecss.extensions.api.AuthorMouseEvent;
import ro.sync.ecss.extensions.api.AuthorMouseListener;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;
import ru.mcfr.oxygen.framework.ui.MOFPopupWindow;

import javax.swing.text.BadLocationException;

public class MOFAuthorMouseListener implements AuthorMouseListener {

    private AuthorAccess authorAccess;

    private MOFPopupWindow popupWindow;

    public MOFAuthorMouseListener(AuthorAccess authorAccess) {
    this.authorAccess = authorAccess;
    // Use the information popup only if this is the standalone Oxygen version.
    if (authorAccess.getWorkspaceAccess().isStandalone()) {
      popupWindow = new MOFPopupWindow(authorAccess, "Position");
    }
    }


    public void mouseClicked(AuthorMouseEvent e) {
        // Display the mouse coordinates.
    if (authorAccess.getWorkspaceAccess().isStandalone()) {

      if (e.clickCount == 2) {
          int offset = authorAccess.getEditorAccess().getCaretOffset();
          try {
              AuthorNode selectedNode = authorAccess.getDocumentController().getNodeAtOffset(offset);
              AuthorElement ae = (AuthorElement) selectedNode;
              int count = ae.getAttributesCount();
              String toDisplay = "";
              for (int i = 0; i < count; i++){
                  String attrName = ae.getAttributeAtIndex(i);
                  String attrValue = ae.getAttribute(attrName).getValue();
                  toDisplay += attrName + ":" + attrValue + "\n";
              }
              popupWindow.setTimeToDisplay(2);
              popupWindow.display(toDisplay, e.X, e.Y, 10);
          } catch (BadLocationException e1) {
              e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          }
      }
    }
    }

    public void mousePressed(AuthorMouseEvent e) {
    }

    public void mouseReleased(AuthorMouseEvent e) {
    }

    public void mouseDragged(AuthorMouseEvent e) {
    }

    public void mouseMoved(AuthorMouseEvent e) {
    }
}
